package com.tang.framework.async;

import com.tang.framework.async.model.Async;
import com.tang.framework.async.model.AsyncLog;
import jodd.util.ThreadFactoryBuilder;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author tang jun liang
 * @Date 2023/5/20 17:14
 */
public abstract class AbstractAsyncHandler<TAsync extends Async> {


    private static final ThreadPoolExecutor ASYNC_THREAD_POLL_EXECUTOR = new ThreadPoolExecutor(3, 9,
            5L, TimeUnit.MINUTES,
            new LinkedBlockingDeque<>(),
            ThreadFactoryBuilder.create()
                    .withDaemon(true)
                    .withNameFormat("async:%s")
                    .get()
    );


    public void async(TAsync async) {
        CompletableFuture.runAsync(() -> this.async0(async), ASYNC_THREAD_POLL_EXECUTOR);
    }

    /**
     * 执行异步操作
     *
     * @param async
     */
    protected abstract void async0(TAsync async);

}
